package com.sky.shortlink.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sky.shortlink.project.convention.result.Result;
import com.sky.shortlink.project.convention.result.Results;
import com.sky.shortlink.project.dto.req.ShortLinkBatchCreateReqDTO;
import com.sky.shortlink.project.dto.req.ShortLinkCreateReqDTO;
import com.sky.shortlink.project.dto.req.ShortLinkPageReqDTO;
import com.sky.shortlink.project.dto.req.ShortLinkUpdateReqDTO;
import com.sky.shortlink.project.dto.resp.ShortLinkBatchCreateRespDTO;
import com.sky.shortlink.project.dto.resp.ShortLinkCreateRespDTO;
import com.sky.shortlink.project.dto.resp.ShortLinkGroupCountQueryRespDTO;
import com.sky.shortlink.project.dto.resp.ShortLinkPageRespDTO;
import com.sky.shortlink.project.service.ShortLinkService;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Blue sky
 * @Description 短链接控制层
 * @create 2024-02-20 9:50
 */
@RestController
@RequiredArgsConstructor
@Slf4j
public class ShortLinkController {
    @Resource
    ShortLinkService shortLinkService;

    /**
     * 创建短链接
     */
    @PostMapping("/api/short-link/v1/create")
    //TODO 改成微服务版本 + 幂等
//    @SentinelResource(   //坑爹，加上@SentinelResource后shortLinkService就为空（自动装配不了），找了好久最后放弃了
//            value = "create_short-link",
//            blockHandler = "createShortLinkBlockHandlerMethod",
//            blockHandlerClass = CustomBlockHandler.class
//    )
    private Result<ShortLinkCreateRespDTO> create(@RequestBody ShortLinkCreateReqDTO createReqDTO){
        return Results.success(shortLinkService.create(createReqDTO));
    }

    /**
     * 修改短链接
     */
    @PostMapping("/api/short-link/v1/update")
    public Result<Void> updateShortLink(@RequestBody ShortLinkUpdateReqDTO requestParam) {
        shortLinkService.updateShortLink(requestParam);
        return Results.success();
    }
    /**
     * 分页查询短链接
     */
    @GetMapping("/api/short-link/v1/page")
    private Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO pageReqDTO){
        return Results.success(shortLinkService.pageShortLink(pageReqDTO));
    }

    /**
     * 统计同一个gid下短链接数量
     */
    @GetMapping("/api/short-link/v1/count")
    public Result<List<ShortLinkGroupCountQueryRespDTO>> listGroupLinkCount(@RequestParam("requestParam") List<String> requestParam){
            return Results.success(shortLinkService.listGroupLinkCount(requestParam));
    }

    /**
     * 短链接跳转
     * @param shortUri  原始短链接
     * @param request 用于获取域名
     * @param response 用于请求重定向
     */
    @GetMapping("/{short-uri}")
    public void restoreUrl(@PathVariable("short-uri") String shortUri, ServletRequest request, ServletResponse response){
        shortLinkService.restoreUrl(shortUri, request, response);
    }

    /**
     * 批量创建短链接
     */
    @PostMapping("/api/short-link/v1/create/batch")
    public Result<ShortLinkBatchCreateRespDTO> batchCreateShortLink(@RequestBody ShortLinkBatchCreateReqDTO requestParam) {
        return Results.success(shortLinkService.batchCreateShortLink(requestParam));
    }



}
